# 设置输出编码为UTF8，以正确显示中文
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
$env:PYTHONIOENCODING = "UTF-8"

# 设置输出文件路径到桌面
$outputFile = [System.IO.Path]::Combine([Environment]::GetFolderPath('Desktop'), "info.txt")
# 创建或清空输出文件
"" | Set-Content $outputFile

# 添加辅助函数
function Write-OutputAndLog {
    param(
        [string]$Message,
        [System.ConsoleColor]$Color = 'White'
    )
    $Message | Add-Content $outputFile
    Write-Host $Message -ForegroundColor $Color
}

Write-OutputAndLog "`n====== 系统检查报告 ======`n" Green

# 定义总步骤数和当前步骤
$totalSteps = 5  # 总共有5个主要检查项
$currentStep = 0

# 1. 检查特定软件的安装情况
$currentStep++
Write-Progress -Activity "系统检查进行中" -Status "步骤 $currentStep/$totalSteps : 检查已安装软件" -PercentComplete (($currentStep / $totalSteps) * 100)
Write-OutputAndLog "1. 已安装软件检查：" Yellow
$installedSoftware = @(
    Get-WmiObject -Class Win32_Product | Select-Object Name;
    Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName;
    Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName;
    Get-Process | Select-Object ProcessName # 添加进程检查
) | ForEach-Object { $_.Name; $_.DisplayName; $_.ProcessName } | Where-Object { $_ -ne $null }

# 检查特定软件（添加更多可能的名称）
$targetSoftware = @{
    "向日葵" = @("Sunlogin", "SunloginClient", "向日葵", "向日葵远程控制")
    "ToDesk" = @("ToDesk", "ToDesk_Service", "ToDesk Remote Desktop")
    "绿盟" = @("绿盟", "NSFOCUS")
}

foreach ($software in $targetSoftware.Keys) {
    $found = $false
    foreach ($name in $targetSoftware[$software]) {
        if ($installedSoftware -match $name) {
            $found = $true
            break
        }
    }
    if ($found) {
        Write-OutputAndLog "  ✓ 已安装: $software" Green
    } else {
        Write-OutputAndLog "  × 未安装: $software" Red
    }
}

# 检查通软进程
if ($processlist.ProcessName -match "通软") {
    Write-OutputAndLog "  ✓ 通软安全管控正在运行" Green
} else {
    Write-OutputAndLog "  × 未检测到通软安全管控进程" Red
}

# 2. 获取网络适配器信息
$currentStep++
Write-Progress -Activity "系统检查进行中" -Status "步骤 $currentStep/$totalSteps : 检查网络适配器" -PercentComplete (($currentStep / $totalSteps) * 100)
Write-OutputAndLog "`n2. 网络适配器信息：" Yellow
Get-NetAdapter | ForEach-Object {
    Write-OutputAndLog "  接口名称: $($_.Name)"
    Write-OutputAndLog "  状态: $($_.Status)"
    Write-OutputAndLog "  MAC地址: $($_.MacAddress)"
    Write-OutputAndLog "  速度: $($_.LinkSpeed)`n"
}

# 3. 检查外网连接
# 3. 检查外网连接和获取公网IP
$currentStep++
Write-Progress -Activity "系统检查进行中" -Status "步骤 $currentStep/$totalSteps : 检查外网连接" -PercentComplete (($currentStep / $totalSteps) * 100)
Write-OutputAndLog "3. 外网连接检查：" Yellow
try {
    $testConnection = Test-NetConnection -ComputerName www.baidu.com -Port 80 -WarningAction SilentlyContinue
    if ($testConnection.TcpTestSucceeded) {
        Write-OutputAndLog "  ✓ 可以访问外网" Green
        
        # 尝试获取公网IP
        try {
            $publicIP = (Invoke-WebRequest -Uri "http://ip-api.com/json/" -UseBasicParsing).Content | ConvertFrom-Json
            Write-OutputAndLog "  公网IP: $($publicIP.query)" Green
            Write-OutputAndLog "  IP位置: $($publicIP.country) $($publicIP.regionName) $($publicIP.city)" Green
        } catch {
            # 如果第一个API失败，尝试备用API
            try {
                $publicIP = (Invoke-WebRequest -Uri "https://api.ipify.org?format=json" -UseBasicParsing).Content | ConvertFrom-Json
                Write-OutputAndLog "  公网IP: $($publicIP.ip)" Green
            } catch {
                Write-OutputAndLog "  无法获取公网IP地址" Yellow
            }
        }
    } else {
        Write-OutputAndLog "  × 无法访问外网" Red
    }
} catch {
    Write-OutputAndLog "  × 无法访问外网" Red
    Write-OutputAndLog "  错误信息: $($_.Exception.Message)" Red
}

# 4. 检查锁屏和屏幕保护设置
$currentStep++
Write-Progress -Activity "系统检查进行中" -Status "步骤 $currentStep/$totalSteps : 检查安全设置" -PercentComplete (($currentStep / $totalSteps) * 100)
Write-OutputAndLog "`n4. 安全设置检查：" Yellow
$screenSaverTimeout = Get-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name ScreenSaveTimeOut -ErrorAction SilentlyContinue
$screenSaverActive = Get-ItemProperty -Path "HKCU:\Control Panel\Desktop" -Name ScreenSaveActive -ErrorAction SilentlyContinue

# 检查 Windows PIN 和密码设置
$pinEnabled = Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\NgcPin"
$passwordEnabled = Get-LocalUser $env:USERNAME | Select-Object -ExpandProperty PasswordRequired

if ($screenSaverTimeout.ScreenSaveTimeOut -gt 0 -and $screenSaverActive.ScreenSaveActive -eq "1") {
    Write-OutputAndLog "  ✓ 已启用屏幕保护程序" Green
} else {
    Write-OutputAndLog "  × 未启用屏幕保护程序" Red
}

if ($pinEnabled -or $passwordEnabled) {
    Write-OutputAndLog "  ✓ 已设置锁屏密码（Windows PIN 或账户密码）" Green
} else {
    Write-OutputAndLog "  × 未设置锁屏密码" Red
}

# 5. 获取IP地址和主机名
$currentStep++
Write-Progress -Activity "系统检查进行中" -Status "步骤 $currentStep/$totalSteps : 获取系统信息" -PercentComplete (($currentStep / $totalSteps) * 100)
Write-OutputAndLog "`n5. 系统信息：" Yellow
$computerInfo = Get-WmiObject Win32_ComputerSystem
$ipAddresses = Get-NetIPAddress | Where-Object { $_.AddressFamily -eq 'IPv4' -and $_.IPAddress -ne '127.0.0.1' }

Write-OutputAndLog "  主机名: $($computerInfo.Name)"
Write-OutputAndLog "  IP地址:"
foreach ($ip in $ipAddresses) {
    Write-OutputAndLog "    $($ip.IPAddress) ($($ip.InterfaceAlias))"
}

# 完成检查
Write-Progress -Activity "系统检查进行中" -Status "检查完成" -Completed
Write-OutputAndLog "`n====== 检查完成 ======`n" Green